<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class It_message extends MY_Controller {
	
	function __construct()
	{
		parent::__construct(); 
		$this->load->model('it_message_model', 'Message');
		
		$this->secure_session();
	}
	
	function index(){
		$this->secure_session();
		
		$data['title'] = "Mailbox";
		
		$data['data'] = null;
		
		viewByTemplate('it_message/index', $data, 'grid');
	}
	
	function getdata()
	{
		echo $this->Message->GetJSON();
	}
	
	function create()
	{
		$data['title'] = "Add Message";
		$data['data'] = NULL;
		$data['data']['from'] = USERID;
		
		$data['alluser'] = $this->Message->GetAllUser();
		$data['mode'] = 'create';
		viewByTemplate('it_message/form', $data, 'form');
	}

	function edit()
	{
		$data['read_status']	= '1';
		$data['modify_date']	= NOW;
		$data['modify_user']	= USERID;
					
		$this->Message->Update($data, array('id'	=> get('id')));
		
		$data['title'] = "Read Message";
		
		
		$data['data'] = $this->Message->GetSingleView(get('id'));
		$data['alluser'] = $this->Message->GetAllUser();
		
		$data['mode'] = 'edit';
		viewByTemplate('it_message/form', $data, 'form');
	}

	
	private function validation()
	{
		$error = new ErrorList();

		// Get Error Template
		$tpl_empty = $this->MessageTemplate("ER001");
		$tpl_duplicate = $this->MessageTemplate("ER002");

		
		
		if (post("to") == '') {
			$error->Add("to", "To", $tpl_empty);
		} 
		
		if (post("message") == '') {
			$error->Add("message", "Message", $tpl_empty);
		} 
		
		
		return $error;
	}
	
	
	function ajax_post()
	{
		$is_error = 0;
		$ls_response = "";
		$ls_genid = "";

		$error = $this->validation();
		try
		{
			if (count( $error->Get() ) > 0) { $is_error = 1; }
			else
			{
				// Fill Post Data
				$data	= post_by_key(array('to',
											'from',
											'subject',
											'message'));
				
				
				
				$data['to'] = substr ($data['to'] , 0, strlen($data['to'])-2 );
				
				if (post("mode") == "create")
				{	
					$data['user_id']			= trim($data['from']);
					$data['read_status']		= '1';
					$data['type']				= 'S';
					$data['create_date']		= NOW;
					$data['create_user']		= USERID;

					$this->Message->Add($data);
					
					$arr_user  = explode(',',$data['to']);
					 for($i = 0; $i < count($arr_user);$i++){
						
						$data['user_id']			= trim($arr_user[$i]);
						$data['read_status']		= '0';
						$data['type']				= 'I';
					
						$data['create_date']		= NOW;
						$data['create_user']		= USERID;
						
						$this->Message->Add($data);
					 }
					
					$tpl_insert		= $this->MessageTemplate("CS001");
					$ls_response	= $tpl_insert['message'];

					//App.audit_trail("CS001", "create/" + (data.role_id ?? ""));

				}
				
			}
		} catch(Exception $e) {
			$is_error = 1; $ls_response = $e->getMessage();
		}
		
		echo $this->PostResponse(json_encode(array(	'is_error'	=> $is_error,
													'fields'	=> $error->GetDictionary(),
													'msg'		=> $ls_response,
													'genid'		=> $ls_genid,
													'form_sender'=> post("form_sender"))
								));
	}

	function ajax_delete()
	{
		$ls_result	= "";
		$Message_id	= get('Message_id');
		$article_no	= get('article_no');
		
		if ($Message_id != "" && $article_no!="")
		{
			// Delete Single
			$ls_result = $this->Message->DeleteSingle($article_no, $Message_id);

		}
		else
		{
			
			$num_row = intval($this->data['num_row']['deleted']);
			
			for ($i = 0; $i < $num_row; $i++)
			{
				$row	= $this->data['deleted'][$i];
				$ls_result .= $this->Message->DeleteSingle($row['article_no'], $row['Message_id']);
			}
		}
		
		echo $ls_result;
	}
	
	function NewMessage(){
		$tot_message = $this->get_data("SELECT count(1) 
								FROM it_message 
								WHERE 
									trim(user_id) ='".USERID."' AND
									type = 'I' AND
									read_status='0'");
									
		echo $tot_message;
	}
}
?>